package cn.lanyi.basic.service.impl;

import cn.lanyi.basic.mapper.BaseMapper;
import cn.lanyi.basic.query.BaseQuery;
import cn.lanyi.basic.service.IBaseService;
import cn.lanyi.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public PageList<T> queryData(BaseQuery baseQuery) {
        // 1.获取到分页总条数
        Integer totals = baseMapper.queryCount(baseQuery);

        // 2.如果查询到的总条数为空，那么就没有必要再查询数据
        if (null == totals || 0 == totals){
            return new PageList<T>(0,new ArrayList<>());
        }

        // 3.获取到分页数据
        List<T> rows = baseMapper.queryData(baseQuery);

        // 4.返回分页对象
        return new PageList<T>(totals, rows);
    }

    @Override
    public T loadById(Long id)   {
        return baseMapper.loadById(id);
    }

    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    @Override
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    public void batchRemove(List<Long> ids) {
        baseMapper.batchRemove(ids);
    }

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }
}
